package com.yihu.wlyy.service.app.account;

import com.yihu.wlyy.entity.*;
import com.yihu.wlyy.entity.address.City;
import com.yihu.wlyy.entity.address.Province;
import com.yihu.wlyy.entity.address.Street;
import com.yihu.wlyy.entity.address.Town;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.repository.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.yihu.wlyy.service.BaseService;
import com.yihu.wlyy.util.EncodesUtil;

/**
 * 患者基本信息类.
 * 
 * @author George
 */
@Component
@Transactional
public class PatientInfoService extends BaseService {

	@Autowired
	private PatientDao patientDao;
	@Autowired
	private ProvinceDao provinceDao;
	@Autowired
	private CityDao cityDao;
	@Autowired
	private TownDao townDao;
	@Autowired
	private StreetDao streetDao;
	@Autowired
	private WlyyPatientInviteLogDao patientInviteLogDao;
	@Autowired
	private TblBasicDao tblBasicDao;
	@Autowired
	private SbInfoDao sbInfoDao;
	/**
	 * 根据患者标识查询患者信息
	 * @param code
	 * @return
	 */
	public Patient findByCode(String code) {
		return patientDao.findByCode(code);
	}

	/**
	 * 根据患者身份证号码查询患者信息
	 * @param idcard
	 * @return
	 */
	public Patient findByIdcard(String idcard) {
		return patientDao.findByIdcard(idcard);
	}

	// 根據病情等级获取患者信息
	public Iterable<Patient> findInfoByLevel(int level) {
		return patientDao.findInfoByLevel(level);
	}

	/**
	 * 更新患者基本信息
	 * @param patient
	 */
	public Patient updatePatient(Patient patient) {
		if (StringUtils.isNotEmpty(patient.getStreet())) {
			Street street = streetDao.findByCode(patient.getStreet());
			patient.setStreetName(street.getName());
		}else{
			patient.setStreetName("");
		}
		Province province = provinceDao.findByCode(patient.getProvince());
		if (province != null) {
			patient.setProvinceName(province.getName());
		}else{
			patient.setProvinceName("");
		}
		City city = cityDao.findByCode(patient.getCity());
		if (city != null) {
			patient.setCityName(city.getName());
		}else{
			patient.setCityName("");
		}
		Town town = townDao.findByCode(patient.getTown());
		if (town != null) {
			patient.setTownName(town.getName());
		}else{
			patient.setTownName("");
		}
		return patientDao.save(patient);
	}

	public Patient updateUser(Patient patient) {
		return patientDao.save(patient);
	}

	/**
	 * 修改患者密码
	 * @param patient
	 */
	public void updatePatientPwd(Patient patient) {
		EncodesUtil.entryptPassword(patient);
		patientDao.save(patient);
	}

	public Patient invilogCode(String invilogCode) {
		WlyyPatientInviteLog patientInviteLog=patientInviteLogDao.findOne(invilogCode);
		String patientCode=patientInviteLog.getInvitePatientCode();
		Patient patient=patientDao.findByCode(patientCode);
		return patient;
	}

	public boolean validatePatient(String name, String idCard, String ssc) {
		TblBasic tblBasic=tblBasicDao.findByIdCard(idCard);
		SbInfo sbInfo= sbInfoDao.findBySfzh18(idCard);
		if(tblBasic!=null&&sbInfo!=null){
			String nameTemp=tblBasic.getName();

		}
		return false;
	}

	public void checkMobile(String mobile) throws Exception{
		Patient p=patientDao.findByMobile(mobile);
		if(p!=null){
			throw  new Exception("手机号已经被注册");
		}
	}
}
